<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="日期" prop="dataDate">
        <el-date-picker
          clearable
          v-model="queryParams.dataDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          :icon="!isOpen ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"
          size="mini"
          @click="shouqi"
          >展开/收起</el-button
        >
      </el-form-item>

      <div v-if="isOpen">
        <el-form-item label="总花费(元)" prop="totalCost">
          <el-input
            v-model.trim="queryParams.totalCost"
            placeholder="请输入总花费(元)"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="成交花费(元)" prop="transactionCost">
          <el-input
            v-model.trim="queryParams.transactionCost"
            placeholder="请输入成交花费(元)"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="交易额(元)" prop="tradingAmount">
          <el-input
            v-model.trim="queryParams.tradingAmount"
            placeholder="请输入交易额(元)"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="实际投产比" prop="actualProductionRatio">
          <el-input
            v-model.trim="queryParams.actualProductionRatio"
            placeholder="请输入实际投产比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="净实际投产比" prop="netActualProductionRatio">
          <el-input
            v-model.trim="queryParams.netActualProductionRatio"
            placeholder="请输入净实际投产比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="净交易额(元)" prop="netTransactionAmount">
          <el-input
            v-model.trim="queryParams.netTransactionAmount"
            placeholder="请输入净交易额(元)"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="净成交笔数" prop="cleanTradeCount">
          <el-input
            v-model.trim="queryParams.cleanTradeCount"
            placeholder="请输入净成交笔数"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="每笔净成交花费(元)" prop="netTransactionCost">
          <el-input
            v-model.trim="queryParams.netTransactionCost"
            placeholder="请输入每笔净成交花费(元)"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="净交易额占比" prop="netTradeAmountRatio">
          <el-input
            v-model.trim="queryParams.netTradeAmountRatio"
            placeholder="请输入净交易额占比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="成交笔数" prop="totalTransactions">
          <el-input
            v-model.trim="queryParams.totalTransactions"
            placeholder="请输入成交笔数"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="每笔成交花费(元)" prop="dealCost">
          <el-input
            v-model.trim="queryParams.dealCost"
            placeholder="请输入每笔成交花费(元)"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="每笔成交金额(元)" prop="aTransactionAmount">
          <el-input
            v-model.trim="queryParams.aTransactionAmount"
            placeholder="请输入每笔成交金额(元)"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="全站推广费比" prop="totalPromotionRate">
          <el-input
            v-model.trim="queryParams.totalPromotionRate"
            placeholder="请输入全站推广费比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="曝光量" prop="exposureVolume">
          <el-input
            v-model.trim="queryParams.exposureVolume"
            placeholder="请输入曝光量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="点击量" prop="clicksCount">
          <el-input
            v-model.trim="queryParams.clicksCount"
            placeholder="请输入点击量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="店铺id" prop="shopId">
          <el-input
            v-model.trim="queryParams.shopId"
            placeholder="请输入店铺id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </div>
      <!-- <el-form-item label="逻辑删除" prop="isDelete">
        <el-input
          v-model.trim="queryParams.isDelete"
          placeholder="请输入逻辑删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item> -->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pdd:BiPddPromotionDwd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pdd:BiPddPromotionDwd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pdd:BiPddPromotionDwd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pdd:BiPddPromotionDwd:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <uploadFileShopid
          @handleSuccess="getList"
          :uploadUrl="'/pdd/BiPddPromotionDwd/importData'"
        />
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="BiPddPromotionDwdList"
      @selection-change="handleSelectionChange"
      :height="650"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="日期" align="center" prop="dataDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总花费(元)" align="center" prop="totalCost" />
      <el-table-column
        label="成交花费(元)"
        align="center"
        prop="transactionCost"
      />
      <el-table-column label="交易额(元)" align="center" prop="tradingAmount" />
      <el-table-column
        label="实际投产比"
        align="center"
        prop="actualProductionRatio"
      />
      <el-table-column
        label="净实际投产比"
        align="center"
        prop="netActualProductionRatio"
      />
      <el-table-column
        label="净交易额(元)"
        align="center"
        prop="netTransactionAmount"
      />
      <el-table-column
        label="净成交笔数"
        align="center"
        prop="cleanTradeCount"
      />
      <el-table-column
        label="每笔净成交花费(元)"
        align="center"
        prop="netTransactionCost"
      />
      <el-table-column
        label="净交易额占比"
        align="center"
        prop="netTradeAmountRatio"
      />
      <el-table-column
        label="成交笔数"
        align="center"
        prop="totalTransactions"
      />
      <el-table-column
        label="每笔成交花费(元)"
        align="center"
        prop="dealCost"
      />
      <el-table-column
        label="每笔成交金额(元)"
        align="center"
        prop="aTransactionAmount"
      />
      <el-table-column
        label="全站推广费比"
        align="center"
        prop="totalPromotionRate"
      />
      <el-table-column label="曝光量" align="center" prop="exposureVolume" />
      <el-table-column label="点击量" align="center" prop="clicksCount" />
      <el-table-column label="店铺id" align="center" prop="shopId" />
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <el-table-column label="逻辑删除" align="center" prop="isDelete" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pdd:BiPddPromotionDwd:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pdd:BiPddPromotionDwd:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改拼多多推广日报对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="日期" prop="dataDate">
              <el-date-picker
                clearable
                v-model="form.dataDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="总花费(元)" prop="totalCost">
              <el-input
                v-model.trim="form.totalCost"
                placeholder="请输入总花费(元)"
              />
            </el-form-item>
            <el-form-item label="成交花费(元)" prop="transactionCost">
              <el-input
                v-model.trim="form.transactionCost"
                placeholder="请输入成交花费(元)"
              />
            </el-form-item>
            <el-form-item label="交易额(元)" prop="tradingAmount">
              <el-input
                v-model.trim="form.tradingAmount"
                placeholder="请输入交易额(元)"
              />
            </el-form-item>
            <el-form-item label="实际投产比" prop="actualProductionRatio">
              <el-input
                v-model.trim="form.actualProductionRatio"
                placeholder="请输入实际投产比"
              />
            </el-form-item>
            <el-form-item label="净实际投产比" prop="netActualProductionRatio">
              <el-input
                v-model.trim="form.netActualProductionRatio"
                placeholder="请输入净实际投产比"
              />
            </el-form-item>
            <el-form-item label="净交易额(元)" prop="netTransactionAmount">
              <el-input
                v-model.trim="form.netTransactionAmount"
                placeholder="请输入净交易额(元)"
              />
            </el-form-item>
            <el-form-item label="净成交笔数" prop="cleanTradeCount">
              <el-input
                v-model.trim="form.cleanTradeCount"
                placeholder="请输入净成交笔数"
              />
            </el-form-item>
            <el-form-item label="每笔净成交花费(元)" prop="netTransactionCost">
              <el-input
                v-model.trim="form.netTransactionCost"
                placeholder="请输入每笔净成交花费(元)"
              />
            </el-form-item>
            <el-form-item label="净交易额占比" prop="netTradeAmountRatio">
              <el-input
                v-model.trim="form.netTradeAmountRatio"
                placeholder="请输入净交易额占比"
              />
            </el-form-item>
          </el-col>
          <!-- <el-form-item label="经办人" prop="procedureOperator">
          <el-input
            v-model.trim="form.procedureOperator"
            placeholder="请输入经办人"
          />
        </el-form-item> -->
          <el-col :span="12">
            <el-form-item label="成交笔数" prop="totalTransactions">
              <el-input
                v-model.trim="form.totalTransactions"
                placeholder="请输入成交笔数"
              />
            </el-form-item>
            <el-form-item label="每笔成交花费(元)" prop="dealCost">
              <el-input
                v-model.trim="form.dealCost"
                placeholder="请输入每笔成交花费(元)"
              />
            </el-form-item>
            <el-form-item label="每笔成交金额(元)" prop="aTransactionAmount">
              <el-input
                v-model.trim="form.aTransactionAmount"
                placeholder="请输入每笔成交金额(元)"
              />
            </el-form-item>
            <el-form-item label="全站推广费比" prop="totalPromotionRate">
              <el-input
                v-model.trim="form.totalPromotionRate"
                placeholder="请输入全站推广费比"
              />
            </el-form-item>
            <el-form-item label="曝光量" prop="exposureVolume">
              <el-input
                v-model.trim="form.exposureVolume"
                placeholder="请输入曝光量"
              />
            </el-form-item>
            <el-form-item label="点击量" prop="clicksCount">
              <el-input
                v-model.trim="form.clicksCount"
                placeholder="请输入点击量"
              />
            </el-form-item>
            <el-form-item label="店铺id" prop="shopId">
              <el-input v-model.trim="form.shopId" placeholder="请输入店铺id" />
            </el-form-item>
            <el-form-item label="逻辑删除" prop="isDelete">
              <el-input
                v-model.trim="form.isDelete"
                placeholder="请输入逻辑删除"
              />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model.trim="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改产品二级信息对话框 :on-change="handleChange"-->
    <el-dialog
      title="导入"
      :visible.sync="dialogVisible"
      width="500px"
      append-to-body
      v-if="dialogVisible"
    >
      <!-- 文件上传-->
      <el-form
        :rules="rules"
        :model="dataForm"
        ref="dataForm"
        label-width="150px"
        @submit.native.prevent
      >
        <!-- <el-form-item label="模板下载" prop="finalIsReturn">
          <el-button size="mini" @click="downLoadTemplateFile">
            导入模板下载
          </el-button>
        </el-form-item> -->
        <el-form-item label="店铺：" prop="name">
          <el-select v-model="dataForm.name" placeholder="店铺" clearable>
            <el-option
              v-for="dict in cascadeList"
              :key="dict.id"
              :label="dict.storeName"
              :value="dict.shopId"
            />
          </el-select>
          <!-- <el-input
            type="text"
            v-model.trim="dataForm.name"
            clearable
          ></el-input> -->
          <!-- <el-cascader
            class="region"
            v-model="dataForm.name"
            placeholder="选择店铺分组"
            :options="cascadeList"
            :props="{ checkStrictly: true }"
            style="width: 300px; height: 30px"
          >
          </el-cascader> -->
        </el-form-item>
        <el-form-item label="文件：" prop="file">
          <el-upload
            class="upload-import"
            ref="uploadImport"
            :http-request="httpRequest"
            action=""
            :on-remove="handleRemove"
            :file-list="fileList"
            :limit="1"
            :on-change="handleChange"
            :auto-upload="false"
            :before-upload="beforeAvatarUpload"
          >
            <!--
            accept="application/zip,.zip" 设置接收的文件类型-->
            <el-button
              v-show="!hasFile"
              slot="trigger"
              size="small"
              type="primary"
              >选取文件</el-button
            >
            <!-- <div slot="tip" class="el-upload__tip">
          只能上传zip文件，且不超过10M
        </div> -->
          </el-upload>
          <!-- 关闭自动上传-->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitUpload">提交</el-button>
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiPddPromotionDwd,
  getBiPddPromotionDwd,
  delBiPddPromotionDwd,
  addBiPddPromotionDwd,
  updateBiPddPromotionDwd,
  selectShopList,
} from "@/api/pdd/BiPddPromotionDwd";
//--------导入token
import { getToken } from "@/utils/auth";
import axios from "axios";
export default {
  name: "BiPddPromotionDwd",
  data() {
    return {
      hasFile: false,
      fileList: [],
      dialogVisible: false,
      pjtUrl: process.env.VUE_APP_BASE_API,

      cascadeList: [],
      dataForm: {
        name: null,
        file: null,
      },

      isOpen: false,
      inputValue: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拼多多推广日报表格数据
      BiPddPromotionDwdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dataDate: null,
        totalCost: null,
        transactionCost: null,
        tradingAmount: null,
        actualProductionRatio: null,
        netActualProductionRatio: null,
        netTransactionAmount: null,
        cleanTradeCount: null,
        netTransactionCost: null,
        netTradeAmountRatio: null,
        totalTransactions: null,
        dealCost: null,
        aTransactionAmount: null,
        totalPromotionRate: null,
        exposureVolume: null,
        clicksCount: null,
        shopId: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getselectShopList() {
      selectShopList().then((response) => {
        this.cascadeList = response;
      });
    },
    //搜索栏展示与收起
    shouqi() {
      // console.log(1);
      this.isOpen = !this.isOpen;
    },
    /** 查询拼多多推广日报列表 */
    getList() {
      this.loading = true;
      listBiPddPromotionDwd(this.queryParams).then((response) => {
        this.BiPddPromotionDwdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dataDate: null,
        totalCost: null,
        transactionCost: null,
        tradingAmount: null,
        actualProductionRatio: null,
        netActualProductionRatio: null,
        netTransactionAmount: null,
        cleanTradeCount: null,
        netTransactionCost: null,
        netTradeAmountRatio: null,
        totalTransactions: null,
        dealCost: null,
        aTransactionAmount: null,
        totalPromotionRate: null,
        exposureVolume: null,
        clicksCount: null,
        shopId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拼多多推广日报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiPddPromotionDwd(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改拼多多推广日报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiPddPromotionDwd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiPddPromotionDwd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除拼多多推广日报编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiPddPromotionDwd(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pdd/BiPddPromotionDwd/export",
        {
          ...this.queryParams,
        },
        `BiPddPromotionDwd_${new Date().getTime()}.xlsx`
      );
    },

    //图片上传前的相关判断
    beforeAvatarUpload(file) {
      // const isJPG = file.type == 'image/jpeg' || file.type == 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 100;
      // if (!isJPG) {
      //     this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
      // }
      if (!isLt2M) {
        this.$message.error("上传导入文件大小不能超过 100MB!");
      }

      return isLt2M;
    },
    handleRemove(file, fileList) {
      if (!fileList.length) {
        this.hasFile = false;
      }
      this.dataForm.file = null;
    },
    handleChange(file, fileList) {
      if (fileList.length >= 2) {
        return;
      }
      if (fileList.length === 1) {
        this.hasFile = true;
      }
      this.dataForm.file = file;
    },
    //确定按钮
    //调用组件upload的submit方法，从而触发httpRequest方法，实现手动上传
    submitUpload() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$refs.uploadImport.submit();
        }
      });
    },
    httpRequest(param) {
      //   if (this.dataForm.name.length < 3) {
      //     this.$modal.msgWarning("请选择到店铺");
      //     return;
      //   }
      let fd = new FormData();
      fd.append("file", param.file); // 传文件
      // fd.append("salesGrade", this.dataForm.name);
      //dataPar.file.raw
      console.log(JSON.stringify(fd));
      this.openLoading();
      axios
        .post(
          process.env.VUE_APP_BASE_API +
            "/pdd/BiPddPromotionDwd/importData?shopId=" +
            this.dataForm.name,
          fd,
          {
            headers: {
              header: { responseType: "blob" },

              Authorization: "Bearer " + getToken(),
              "Content-Type": "multipart/form-data",
            }, //定义内容格式,很重要
            // timeout: 20000,
          }
        )
        .then((response) => {
          console.log(response);
          if (response.data.code == 200) {
            this.dialogVisible = false;
            this.$modal.closeLoading();
            this.$modal.msgSuccess("导入成功");
            this.hasFile = false;
            this.getList();
          }
        });
    },
    //部门文件导入修改打开
    daoru() {
      this.getselectShopList();
      this.dialogVisible = true;
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在导入，请稍候！");
    },
  },
};
</script>
